package com.example.ck.controller;

import com.example.ck.entity.Project;
import com.example.ck.service.ProjectService;
import com.example.ck.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 项目申报表(Project)表控制层
 *
 * @author makejava
 * @since 2024-05-01 12:14:44
 */
@Slf4j
@RestController
@RequestMapping("project")
@CrossOrigin
public class ProjectController {
    @Autowired
    private ProjectService projectService;


    /**
     * 新增项目
     */
    @PostMapping("/add")
    public ResultVo addProject(@RequestBody Project project) {
        boolean b = projectService.addProject(project);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("新项目申请成功");
            vo.setSuccess(true);
            vo.setData(project);
            vo.setCode(200);
        } else {
            vo.setMessage("新项目申请失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 根据id查询项目
     */
    @GetMapping("/findById")
    public ResultVo findProjectById(@RequestParam("id") Integer id) {
        Project project = projectService.findById(id);

        ResultVo vo = new ResultVo<>();
        if (project != null) {
            vo.setMessage("查询成功");
            vo.setSuccess(true);
            vo.setData(project);
            vo.setCode(200);
        } else {
            vo.setMessage("查询失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 根据姓名查询数据
     */
    @GetMapping("/findByName")
    public ResultVo findByName(@RequestParam("username") String username) {
        List<Project> projects = projectService.findProjectByUserName(username);

        ResultVo vo = new ResultVo<>();
        if (projects.size() > 0) {
            vo.setMessage("查询成功");
            vo.setSuccess(true);
            vo.setData(projects);
            vo.setCode(200);
        } else {
            vo.setMessage("查询失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 根据id删除项目
     */
    @DeleteMapping("/delete")
    public ResultVo deleteProject(Integer id) {
        boolean b = projectService.deleteProjectById(id);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("删除成功");
            vo.setSuccess(true);
            vo.setCode(200);
        } else {
            vo.setMessage("删除失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 根据id修改项目状态
     */
    @PutMapping("/updateStatus")
    public ResultVo updateStatus(Integer id, String status) {
        boolean b = projectService.updateProjectStatusById(id, status);

        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("修改成功");
            vo.setSuccess(true);
            vo.setCode(200);
        } else {
            vo.setMessage("修改失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 招标商获取审核通过的项目
     */
    @GetMapping("/findByStatus")
    public ResultVo findByStatus() {
        List<Project> projectByStatus = projectService.findProjectByTenderStatus();
        return new ResultVo<>(projectByStatus);
    }

    /**
     * 根据姓名项目状态查询项目
     */
    @GetMapping("/findByNameAndStatus")
    public ResultVo findByNameAndStatus(String username, String status) {
        List<Project> projectByNameAndStatus = projectService.findProjectByUserNameAndStatus(username, status);
        return new ResultVo<>(projectByNameAndStatus);
    }

    /**
     * 修改项目招标状态
     */
    @PutMapping("/updateBidStatus")
    public ResultVo updateBidStatus(Integer id, String tenderStatus) {
        boolean b = projectService.updateProjectTenderStatusById(id, tenderStatus);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("修改成功");
            vo.setSuccess(true);
            vo.setCode(200);
        } else {
            vo.setMessage("修改失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }
}